package com.zhuliwei.job;

import com.zhuliwei.constants.RabbitConstants;
import com.zhuliwei.utils.RedisCache;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @author 朱力维
 * @time 2024/3/8 23:01
 **/
@Component
public class RabbitJob {
    @Resource
    private RedisCache redisCache;

    @Resource
    private RabbitTemplate rabbitTemplate;

    // 每5秒执行一次
    @Scheduled(cron = "*/5 * * * * ?")
    public void updateViewCount() {
        Map<String, Object> map = redisCache.getCacheMap(RabbitConstants.RABBIT_RENEW);

        Set<Map.Entry<String, Object>> entries = map.entrySet();
        // 重新投递注册成功通知发送失败的消息
        entries.stream()
                .filter(stringObjectEntry -> stringObjectEntry.getKey().startsWith(RabbitConstants.EMAIL_MESSAGE_PREMIX))
                .forEach(stringObjectEntry -> {
                    rabbitTemplate.convertAndSend("directExchange", "email.message", stringObjectEntry.getValue(), new CorrelationData(stringObjectEntry.getKey()));
                });

    }

}
